package cn.codingjc.framework.common.core.status.core;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName StatusMechine
 * @Description Todo
 * @Author jccoding
 * @Date 2024/3/5 17:22
 * @Version 1.0
 **/
public class StatusMachine<S extends BaseStatus, E extends BaseEvent>{

    /**
     * S:sourse status
     * E:event
     * S:target status
     */
    private final Map<StatusEventPair<S, E> , S> statusEventPairSMap = new HashMap<>();

    /**
     * 接受当前状态下，指定触发事件，可以达到的目标状态
     * @param sourceStatus
     * @param event
     * @param targetStatus
     */
    public void accept(S sourceStatus, E event, S targetStatus){
        statusEventPairSMap.put(new StatusEventPair<>(sourceStatus, event), targetStatus);
    }

    /**
     * 获取目标状态
     * @param sourceStatus
     * @param event
     * @return
     */
    public S getTargetStatus(S sourceStatus, E event){
        return statusEventPairSMap.get(new StatusEventPair<>(sourceStatus, event));
    }
}
